package interfaces.ltu;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.text.format.DateUtils;

public class AccessAndroidCalender {

	public static HashSet<String> getCalender(Context context) {
    	String str = "***";
    	//HashMap<String> classRoomNos = new HashMap<String>();
    	HashSet<String> classRoomNos = new HashSet<String>();
    	
    	//ContextWrapper context;
		ContentResolver contentResolver = context.getContentResolver();

		// Fetch a list of all calendars synced with the device, their display names and whether the
		// user has them selected for display.
		
		/*final Cursor cursor = contentResolver.query(Uri.parse("content://com.android.calendar/calendars"),
				(new String[] { "_id", "displayName", "selected" }), null, null, null);
		// For a full list of available columns see http://tinyurl.com/yfbg76w

		HashSet<String> calendarIds = new HashSet<String>();
		
		while (cursor.moveToNext()) {

			final String _id = cursor.getString(0);
			final String displayName = cursor.getString(1);
			final Boolean selected = !cursor.getString(2).equals("0");
			str=str+"Id: " + _id + " Display Name: " + displayName + " Selected: " + selected+"\n";
					
			System.out.println("Id: " + _id + " Display Name: " + displayName + " Selected: " + selected);
			if (_id =="2")
			{
			calendarIds.add(_id);
			}
		}*/
    	
		//for (String id : calendarIds) {
		{
			Uri.Builder builder = Uri.parse("content://com.android.calendar/instances/when").buildUpon();
			long now = new Date().getTime();
			ContentUris.appendId(builder, now - DateUtils.WEEK_IN_MILLIS);
			ContentUris.appendId(builder, now + DateUtils.WEEK_IN_MILLIS);
			//ContentUris.appendId(builder, now - DateUtils.DAY_IN_MILLIS);
			//ContentUris.appendId(builder, now + DateUtils.DAY_IN_MILLIS);

		//	Cursor eventCursor = contentResolver.query(builder.build(),	new String[] { "title", "begin", "end", "allDay"}, "Calendars._id=" + id,
			//		null, "startDay ASC, startMinute ASC");
			Cursor eventCursor = contentResolver.query(builder.build(),	new String[] { "title", "begin", "end", "allDay"}, "Calendars._id=2",
							null, "startDay ASC, startMinute ASC"); 
				
			// For a full list of available columns see http://tinyurl.com/yfbg76w

			while (eventCursor.moveToNext()) {
				final String title = eventCursor.getString(0);
				
				final Date begin = new Date(eventCursor.getLong(1));
				final Date end = new Date(eventCursor.getLong(2));
				final Boolean allDay = !eventCursor.getString(3).equals("0");
				
				String[] temp;
				String delimiter = "\n";
				String CourseNo="";
				temp = title.split(delimiter);
				  /* print substrings */
				System.out.println("Print Started");
				  for(int i =0; i < temp.length ; i++)
				  {  
				    System.out.println(temp[i]);
				    if (i==0)
				    {
				    	CourseNo=temp[i];
				    	System.out.println("CourseNo:"+CourseNo);
				    }	
				    if(i==2)
				    {	
				    	String t[]=temp[i].split(",");
				    	if(t.length>0) 
				    	{
				    		for(int j=0;j<t.length;j++)
				    		{
				    			if(AndroidInterfaceActivity.courseLists.contains(CourseNo))
				    			{	
				    				classRoomNos.add(t[j].trim());
				    				System.out.println("Added "+t[j]);
				    			}	
				    		}	
				    	}else
				    	{	
				    		classRoomNos.add(temp[i].trim());
				    		System.out.println("Added "+temp[i]);
				    	}
				    }
				  }
				   System.out.println("Print Ended");
				str=str+"Title: " + title + " Begin: " + begin + " End: " + end +" All Day: " + allDay+"\n";
				//System.out.println("Title: " + title + " Begin: " + begin + " End: " + end +" All Day: " + allDay);
			}
		}
    	return classRoomNos;
    }
}
